package co.yixiang.yshop.module.shop.service.material;

import cn.hutool.core.util.StrUtil;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.shop.controller.admin.material.vo.*;
import co.yixiang.yshop.module.shop.convert.material.MaterialConvert;
import co.yixiang.yshop.module.shop.dal.dataobject.material.MaterialDO;
import co.yixiang.yshop.module.shop.dal.mysql.material.MaterialMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.shop.enums.ErrorCodeConstants.MATERIAL_NOT_EXISTS;

/**
 *  素材库 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class MaterialServiceImpl implements MaterialService {

    @Resource
    private MaterialMapper materialMapper;

    @Override
    public Long createMaterial(MaterialCreateReqVO createReqVO) {
        // 插入
        MaterialDO material = MaterialConvert.INSTANCE.convert(createReqVO);
        if(StrUtil.isBlank(material.getName())){
            material.setName(createReqVO.getUrl().substring(createReqVO.getUrl().lastIndexOf("/") + 1));
        }
        materialMapper.insert(material);
        // 返回
        return material.getId();
    }

    @Override
    public void updateMaterial(MaterialUpdateReqVO updateReqVO) {
        // 校验存在
        validateMaterialExists(updateReqVO.getId());
        // 更新
        MaterialDO updateObj = MaterialConvert.INSTANCE.convert(updateReqVO);
        materialMapper.updateById(updateObj);
    }

    @Override
    public void deleteMaterial(Long id) {
        // 校验存在
        validateMaterialExists(id);
        // 删除
        materialMapper.deleteById(id);
    }

    private void validateMaterialExists(Long id) {
        if (materialMapper.selectById(id) == null) {
            throw exception(MATERIAL_NOT_EXISTS);
        }
    }

    @Override
    public MaterialDO getMaterial(Long id) {
        return materialMapper.selectById(id);
    }

    @Override
    public List<MaterialDO> getMaterialList(Collection<Long> ids) {
        return materialMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MaterialDO> getMaterialPage(MaterialPageReqVO pageReqVO) {
        return materialMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MaterialDO> getMaterialList(MaterialExportReqVO exportReqVO) {
        return materialMapper.selectList(exportReqVO);
    }

    @Override
    public void batchUpdateGroup(MaterialBatchUpdateGroupReqVO materialBatchUpdateGroupReqVO) {
        List<Long> ids = materialBatchUpdateGroupReqVO.getIds();
        for (Long id : ids) {
            MaterialDO materialDO = new MaterialDO();
            materialDO.setId(id);
            materialDO.setGroupId(materialBatchUpdateGroupReqVO.getGrouupId());
            materialMapper.updateById(materialDO);
        }
    }

    @Override
    public void createBatch(MaterialCreateReqVO createReqVO) {
        List<String> urlList = createReqVO.getUrlList();
        List<MaterialDO> list = urlList.stream().map(url -> {
            String name = createReqVO.getName();

            MaterialDO materialDO = new MaterialDO();
            if(StrUtil.isBlank(name)){
                name = url.substring(url.lastIndexOf("/") + 1);
            }else {
                name = name + "-" + url.substring(url.lastIndexOf("/") + 1);
            }
            materialDO.setName(name);
            materialDO.setUrl(url);
            materialDO.setGroupId(createReqVO.getGroupId());
            materialDO.setType(createReqVO.getType());
            return materialDO;
        }).collect(Collectors.toList());
        materialMapper.insertBatch(list);
    }

}
